package com.joelemon.mockinfluxdbdata.datamock.v2.mockunit;

import com.joelemon.mockinfluxdbdata.common.SystemException;
import com.joelemon.mockinfluxdbdata.datamock.v2.enums.NumericalStrategy;
import com.joelemon.mockinfluxdbdata.datamock.v2.interfaces.MockValueUnit;
import lombok.Data;
import org.springframework.beans.BeanUtils;

/**
 * @Description: 所有unit属性的集合，方便编译
 * @Author: HJY
 * @Date: 2023/11/30
 */
@Data
public class DefaultMockValueUnit {

    private String unitName;
    private Integer precision;
    private NumericalStrategy strategy;
    private String range;
    private MockNumericalValUnit factor;
    private Integer loop;
    private MockNumericalValUnit refix;

    public MockValueUnit parseUnit() {
        if (unitName == null) {
            throw new SystemException("unitName is null");
        }
        try {
            Class<?> unitClas = Class.forName(unitName);
            Object instance = unitClas.getConstructor().newInstance();
            BeanUtils.copyProperties(this, instance);
            return (MockValueUnit) instance;
        } catch (ClassNotFoundException e) {
            throw new SystemException("unitName is not exist" + e);
        } catch (Exception e) {
            throw new SystemException("unitName parse error" + e);
        }
    }

}
